<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Student extends CI_Controller{
    /**
     * 选课模板
     */
    public function course_select_panel($page="1-6.html"){
        session_start();
        $this->load->helper("url");
        $base=$this->config->item("base_url");
        $this->load->library('common');

        $pageNumber=$this->common->getPage($page);
        $this->load->model('m_teacher','teacher',true);

        //获取选修课程列表
        $this->load->model("m_course","course",true);
        $result=$this->course->search_course_info(array('style'=>'2'));


        $semester[1]="大一上学期";
        $semester[2]="大一下学期";
        $semester[3]="大二上学期";
        $semester[4]="大二下学期";
        $semester[5]="大三上学期";
        $semester[6]="大三下学期";
        $semester[7]="大四上学期";
        $semester[8]="大四下学期";

        $weekday[1]="星期一";
        $weekday[2]="星期二";
        $weekday[3]="星期三";
        $weekday[4]="星期四";
        $weekday[5]="星期五";
        $weekday[6]="星期六";
        $weekday[7]="星期日";

        $section[1]="第一节";
        $section[2]="第二节";
        $section[3]="第三节";
        $section[4]="第四节";
        $section[5]="第五节";
        $this->load->model("m_college","college",true);

        if($result){
            foreach ($result as $key => $value) {
                if($value->t_id){
                    $teacher=$this->teacher->teacher_search(array('t_id'=>$value->t_id));
                }else{
                    $teacher='';
                }
                if($teacher){
                    $value->teacher=$teacher[0]->name;
                }else{
                    $value->teacher="无";
                }
                $value->weekday=$weekday[$value->weekday];
                $value->section=$section[$value->section];

                //选修人
                if($value->s_id){
                    $elective_student=$this->common->getElectiveStudent($value->s_id);
                    if(in_array($_SESSION['id'],$elective_student)){
                        $elective_course[]=$value;
                    }else{
                        $elective_course='';
                    }
                }else{
                    $elective_course='';
                }
            }
            $pageInfo=$this->common->pageInfo($pageNumber[0],$pageNumber[1],$result);
        }else{
            $pageInfo='';
            $elective_course='';
        }
        echo $this->load->view("admin/elective_course_panel",array("base"=>$base,"pageInfo"=>$pageInfo,"elective_course"=>$elective_course));
    }
    /**
     * 选择选修
     * @param  [type] $param [description]
     * @return [type]        [description]
     */
    public function elective_course_select($param){
        session_start();
        $this->load->helper('url');
        $base=$this->config->item('base_url');
        $this->load->library('common');

        $success=true;

        $this->load->model('m_course','course',true);
        $plan_result=$this->course->course_plan_search(array('id'=>$param),'2');
        if($plan_result[0]->now_count<$plan_result[0]->count){
            if($plan_result[0]->s_id){
               $elective_student=$this->common->getElectiveStudent($plan_result[0]->s_id);
               if(in_array($_SESSION['id'],$elective_student)){
                    $msg="已选该课程！";
                    $success=false;
               }else{
                    $plan_result[0]->s_id=$plan_result[0]->s_id."-".$_SESSION['id'];
               }
            }else{
                $plan_result[0]->s_id=$_SESSION['id'];
            }
            if($success){
                if($this->course->elective_count_update('1',$plan_result[0]->s_id,array('id'=>$param))){
                    $msg="选课成功！";
                }else{
                    $msg="选课失败！";
                }
            }
        }else{
            $msg="人数已满！";
        }
        echo json_encode(array('success'=>$success,'msg'=>$msg));
    }
    /**
     * 取消选修
     * @param  [type] $param [description]
     * @return [type]        [description]
     */
    public function elective_course_cancel($param){
        session_start();
        $this->load->helper('url');
        $base=$this->config->item('base_url');
        $this->load->library('common');

        $success=true;

        $this->load->model('m_course','course',true);
        $plan_result=$this->course->course_plan_search(array('id'=>$param),'2');
        if($plan_result[0]->now_count>'0'){
            if($plan_result[0]->s_id){
               $elective_student=$this->common->getElectiveStudent($plan_result[0]->s_id);
               if(in_array($_SESSION['id'],$elective_student)){
                    foreach ($elective_student as $key => $value) {
                        if($value==$_SESSION['id']){
                            array_splice($elective_student,$key,1);
                        }
                    }
                    $plan_result[0]->s_id=implode('-',$elective_student);
               }else{
                    $msg="没有选择该课程！";
                    $success=false;
               }
            }else{
                $msg="没有选择该课程！";
                $success=false;
            }
            if($success){
                if($this->course->elective_count_update('2',$plan_result[0]->s_id,array('id'=>$param))){
                    $msg="退课成功！";
                }else{
                    $msg="退课失败！";
                }
            }
        }else{
            $msg="人数为0！";
        }
        echo json_encode(array('success'=>$success,'msg'=>$msg));
    }
    /**
     * 课表查询模板
     * @return [type] [description]
     */
    public function course_timetable_panel(){
        session_start();
        $this->load->helper('url');
        $base=$this->config->item('base_url');

        echo $this->load->view('admin/course_timetable_panel',array('base'=>$base));
    }
    /**
     * 课表查询
     * @return [type] [description]
     */
    public function course_timetable_result(){
        session_start();
        $this->load->helper('url');
        $base=$this->config->item('base_url');

        $msg='';
        $success=true;
        $semester=@$_POST['semester']?@$_POST['semester']:'';
        if(!$semester){
            $success=false;
            $msg="请选择学期!";
            $panel='';
        }
        if($success){
            if($_SESSION['classify']!='2'){
                $success=false;
                $msg="您不是学生，没有课表信息！";
                $panel='';
            }
        }
        if($success){
            $this->load->model('m_student','student',true);
            $this->load->model('m_course','course',true);
            $this->load->model('m_teacher','teacher',true);

            $course_timetable=array();
            $student_info=$this->student->student_search(array('s_id'=>$_SESSION['id']));
            $course_list=$this->course->search_course_info(array('academy'=>$student_info[0]->academy,'major'=>$student_info[0]->major,'period'=>$student_info[0]->period,'semester'=>$semester),'1');
            if($course_list){
                foreach ($course_list as $key => $value) {
                    $teacher=$this->teacher->teacher_search(array('t_id'=>$value->t_id));
                    $course_timetable[$value->section][$value->weekday]=array('timeLong'=>$value->timeLong,'place'=>$value->place,'name'=>$value->name,'teacher'=>$teacher[0]->name);
                }
            }
            for ($i=1; $i < 6; $i++) {
                for ($j=1; $j < 8; $j++) {
                    if(!isset($course_timetable[$i][$j])){
                        $course_timetable[$i][$j]='';
                    }
                }
                ksort($course_timetable[$i],SORT_NUMERIC);
            }
            ksort($course_timetable,SORT_NUMERIC);
            $panel=$this->load->view('admin/course_timetable_result',array('base'=>$base,'course_list'=>$course_list,'course_timetable'=>$course_timetable),true);
        }
        echo json_encode(array('success'=>$success,'msg'=>$msg,'panel'=>$panel));
    }
}